package com.lenovo.dh.voteclient.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

import android.util.Log;

public class HttpUtils {
	public static String sendGetRequest(String path, Map<String, String> params, String enc) throws Exception {
		StringBuilder sb = new StringBuilder(path);
		sb.append('&');
		// ?method=save&title=435435435&timelength=89&
		for (Map.Entry<String, String> entry : params.entrySet()) {
			sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');
		}
		sb.deleteCharAt(sb.length() - 1);
		URL url = new URL(sb.toString());
		Log.d("shiqc1", "the final url :" + sb.toString());
		HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5 * 1000);
		String line = null;
		if (conn.getResponseCode() == 200) {
			Log.d("shiqc1", "sendGetRequest:success" );
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			line = reader.readLine();
			/*while ((line = reader.readLine()) != null) {
//				if (line.startsWith("group")) {
//					break;
//				} else if(line.startsWith("300")) {
//					return null;
//				}
			}*/
			reader.close();
		}
		Log.d("shiqc1", "response line :" + line );
		conn.disconnect();
		return line;
	}
}	
